package com.zdxf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zdxf.domain.UpdatePasswordDTO;
import com.zdxf.domain.UserDO;
import com.zdxf.domain.UserRoleDO;
import com.zdxf.mapper.MenuMapper;
import com.zdxf.mapper.UserMapper;
import com.zdxf.param.RoleParam;
import com.zdxf.param.UserParam;
import com.zdxf.service.UserService;
import com.zdxf.util.JwtTokenUtil;
import com.zdxf.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private com.zdxf.util.JwtTokenUtil JwtTokenUtil;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;


    /**
     * 角色列表
     */
    @Override
    public Result listRole(RoleParam roleParam) {
        List<UserRoleDO> list = userMapper.listRole(roleParam);
        int total = userMapper.getListRoleCount();
        JSONObject jsonData = new JSONObject(true);
        jsonData.put("total", total);
        jsonData.put("list", list);
        return new Result(true, 20000, "success", jsonData);
    }

    /**
     * 角色列表不分页
     */
    @Override
    public Result getAllRoles() {
        return new Result(true, 20000, "success", userMapper.getAllRoles());
    }

    /**
     * 新增角色
     */
    @Override
    public Result addRole(UserRoleDO userRole) {
        if (userMapper.checkRole(userRole) == null) {
            return new Result(true, 20000, "success", userMapper.addRole(userRole));
        } else {
            return new Result(false, 50000, "角色名重复", "");
        }
    }

    /**
     * 修改角色
     */
    @Override
    public Result updateRole(UserRoleDO userRole) {
        UserRoleDO res = userMapper.checkRole(userRole);
        if (res == null || res.getId() == userRole.getRoleId()) {
            return new Result(true, 20000, "success", userMapper.updateRole(userRole));
        } else {
            return new Result(false, 50000, "角色已存在", "");
        }
    }

    /**
     * 删除角色
     */
    @Override
    public Result deleteRole(long roleId) {
        List list = userMapper.getUseRole(roleId);
        if (list.size() == 0) {
            return new Result(true, 20000, "success", userMapper.deleteRole(roleId));
        } else {
            return new Result(false, 50000, "当前角色正在使用", list);
        }
    }

    /**
     * 获取在使用角色的用户
     */
    @Override
    public Result getUseRole(long roleId) {
        List list = userMapper.getUseRole(roleId);
        return new Result(true, 20000, "success", list);
    }

    /**
     * 查询用户列表
     */
    @Override
    public Result userList(UserParam userParam) {
        List<UserDO> list = userMapper.userList(userParam);
        int total = userMapper.getListUserCount(userParam);
        JSONObject jsonData = new JSONObject(true);
        jsonData.put("total", total);
        jsonData.put("list", list);
        return new Result(true, 20000, "success", jsonData);
    }

    /**
     * 新增用户
     */
    @Override
    public Result addUser(UserDO userDO) {
        if (userMapper.checkUser(userDO) == null) {
            return new Result(true, 20000, "success", userMapper.addUser(userDO));
        } else {
            return new Result(false, 50000, "用户已存在", "");
        }
    }

    /**
     * 编辑用户
     */
    @Override
    public Result updateUser(UserDO userDO) {
        return new Result(true, 20000, "success", userMapper.updateUser(userDO));
    }

    /**
     * 获取用户详情
     */
    @Override
    public Result getInfo(String token) {
        String username = JwtTokenUtil.getUsernameFromToken(token);
        UserDO userInfo = userMapper.getInfo(username);
        List menuList;
        if (userInfo.getId() == 1) {
            menuList = menuMapper.getList();

        } else {
            menuList = userMapper.getUserMenu(userInfo);
        }
        System.out.println("-----------");
        System.out.println(userInfo);
        System.out.println(menuList);
        System.out.println("-----------");
        userInfo.setMenuList(menuList);
        return new Result(true, 20000, "success", userInfo);
    }

    @Override
    public Result updatePassword(UpdatePasswordDTO updatePasswordDTO) {
        UserDO userInfo = userMapper.getInfoById(updatePasswordDTO.getUserId());
        if (userInfo.getPassword().equals(updatePasswordDTO.getOriginalPassword())) {
            return new Result(true, 20000, "success", userMapper.updatePassword(updatePasswordDTO));
        } else {
            return new Result(false, 50000, "原始密码错误！", null);

        }
    }
}
